package com.rahwi.rahwi_server.Service.impl;

import com.rahwi.rahwi_server.DAO.*;
import com.rahwi.rahwi_server.Entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.sql.Date;


@Service
public class CommentService {

    @Autowired
    private PostDAO postDAO;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private ReplyDAO replyDAO;

    @Autowired
    private CommentDAO commentDAO;

    @Autowired
    private CommentPicturesDAO commentPicturesDAO;

    //新建楼层
    public boolean AddComment(int uid, int pid, String[] pictures, String content) {

        Comment newComment = new Comment();
        newComment.setUid(uid);
        newComment.setPid(pid);
        Date date = new Date(System.currentTimeMillis());
        newComment.setDateTime(date);
        newComment.setPicture(pictures[0]);
        newComment.setDetail(content);
        Specification<Comment> specification = (Specification<Comment>) (root, criteriaQuery, criteriaBuilder)
                -> criteriaBuilder.and(criteriaBuilder.equal(root.get("Pid"), pid));
        int position = commentDAO.findAll(specification).size() + 1;
        newComment.setPosition(position);
        commentDAO.save(newComment);
        Specification<Comment> GetNewComment = (Specification<Comment>) (root, criteriaQuery, criteriaBuilder)
                -> criteriaBuilder.and(criteriaBuilder.equal(root.get("Pid"), pid), criteriaBuilder.equal(root.get("Uid"), uid), criteriaBuilder.equal(root.get("Position"), position));
        int cid = commentDAO.findAll(GetNewComment).get(0).getCid();
        for (String picture :
                pictures) {
            CommentPictures commentPictures = new CommentPictures();
            commentPictures.setCid(cid);
            Specification<CommentPictures> GetCommentPictures = (Specification<CommentPictures>) (root, criteriaQuery, criteriaBuilder)
                    -> criteriaBuilder.and(criteriaBuilder.equal(root.get("Cid"), cid));
            int picturePosition = commentPicturesDAO.findAll(GetCommentPictures).size() + 1;
            commentPictures.setPosition(picturePosition);
            commentPictures.setPicture(picture);
            commentPicturesDAO.save(commentPictures);
        }
        return true;
    }

    //新建回复,目标楼层
    public boolean ReplyToComment(int cid, int uid, String content){
        Reply reply = new Reply();
        reply.setContent(content);
        reply.setTime(new Date(System.currentTimeMillis()));

        Specification<Reply> specification = (Specification<Reply>) (root, criteriaQuery, criteriaBuilder)
                -> criteriaBuilder.and(criteriaBuilder.equal(root.get("CommentId"), cid));
        int no = replyDAO.findAll(specification).size() + 1;
        reply.setPosition(no);

        Specification<Comment> findComment = (Specification<Comment>) (root, criteriaQuery, criteriaBuilder)
                -> criteriaBuilder.and(criteriaBuilder.equal(root.get("CommentId"), cid));
        Comment comment = commentDAO.findById(cid).get();

        reply.setPid(comment.getPid());
        reply.setCommentId(cid);
        reply.setSenderId(uid);
        reply.setReceiverId(comment.getUid());
        reply.setUser(userDAO.findById(comment.getUid()).get());
        reply.setUid(comment.getUid());

        replyDAO.save(reply);
        return true;
    }

    //新建回复，目标楼中楼
    public boolean ReplyToReply(int cid, int uid, int rid, String content){
        Reply reply = new Reply();
        reply.setContent(content);
        reply.setTime(new Date(System.currentTimeMillis()));

        Specification<Reply> specification = (Specification<Reply>) (root, criteriaQuery, criteriaBuilder)
                -> criteriaBuilder.and(criteriaBuilder.equal(root.get("CommentId"), cid));
        int no = replyDAO.findAll(specification).size() + 1;
        reply.setPosition(no);

        Specification<Comment> findComment = (Specification<Comment>) (root, criteriaQuery, criteriaBuilder)
                -> criteriaBuilder.and(criteriaBuilder.equal(root.get("CommentId"), cid));
        Comment comment = commentDAO.findById(cid).get();


        reply.setPid(comment.getPid());
        reply.setCommentId(cid);
        reply.setSenderId(uid);
        reply.setReceiverId(rid);
        reply.setUser(userDAO.findById(comment.getUid()).get());
        reply.setUid(comment.getUid());

        replyDAO.save(reply);
        return true;
    }
}
